﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VOL;
using BLL;

namespace Web.Admin
{
    public partial class DMHangForm : System.Web.UI.Page
    {
        DMHangBL _DMHangBL = new DMHangBL();
        int MaHang = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();

                string _MaHang = Request.QueryString["MaHang"];

                if (_MaHang == null || _MaHang == "" || !int.TryParse(_MaHang, out MaHang))
                {
                    hdfMaHang.Value = "0";
                }
                else
                {
                    DMHang obj = new DMHang();
                    obj.MaHang = MaHang;
                    DataTable dt = _DMHangBL.searchIDDMHang(obj);
                    txtTenHang.Text = dt.Rows[0]["TenHang"].ToString();
                    txtMoTa.Text = dt.Rows[0]["MoTa"].ToString();
                    txtDVT.Text = dt.Rows[0]["DVT"].ToString();
                    txtDonGia.Text = dt.Rows[0]["DonGia"].ToString();
                    hdfMaHang.Value = _MaHang;
                    ddlLoaiHang.SelectedValue = dt.Rows[0]["MaLoaiHang"].ToString();
                    ddlNhaCungCap.SelectedValue = dt.Rows[0]["MaNhaCungCap"].ToString();
                    Image1.ImageUrl = dt.Rows[0]["HinhAnh"].ToString();
                }
            }
        }

        private void FillData()
        {
            NhaCungCapBL _nccBl = new NhaCungCapBL();
            ddlNhaCungCap.DataSource = _nccBl.GetData();
            ddlNhaCungCap.DataValueField = "MaNhaCungCap";
            ddlNhaCungCap.DataTextField = "TenNhaCungCap";
            ddlNhaCungCap.DataBind();
            ddlNhaCungCap.Items.Insert(0, "- Vui lòng Chọn -");

            LoaiHangBL _loaHangBl = new LoaiHangBL();
            ddlLoaiHang.DataSource = _loaHangBl.GetData();
            ddlLoaiHang.DataValueField = "MaLoaiHang";
            ddlLoaiHang.DataTextField = "TenLoaiHang";
            ddlLoaiHang.DataBind();
            ddlLoaiHang.Items.Insert(0, "- Vui lòng Chọn -");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string _MaHang = hdfMaHang.Value;

            string path = "~/images/Upload/";
            string imgageFile = Image1.ImageUrl.ToString();

            if (FileUpload1.HasFile)
            {
                imgageFile = path + FileUpload1.FileName;
                FileUpload1.SaveAs(Server.MapPath(imgageFile));
            }

            if (_MaHang == null || _MaHang == "0" || _MaHang == "")
            {
                // thực hiện thêm mới
                DMHang obj = new DMHang();
                obj.TenHang = txtTenHang.Text;
                obj.MoTa = txtMoTa.Text;
                obj.DVT = txtDVT.Text;
                obj.DonGia = float.Parse(txtDonGia.Text);
                obj.MaLoaiHang = int.Parse(ddlLoaiHang.SelectedValue);
                obj.MaNhaCungCap = int.Parse(ddlNhaCungCap.SelectedValue);
                obj.HinhAnh = imgageFile;

                _DMHangBL.insertDMHang(obj);
            }
            else
            {
                // thực hiện sửa
                DMHang obj = new DMHang();
                obj.MaHang = int.Parse(_MaHang);
                obj.TenHang = txtTenHang.Text;
                obj.MoTa = txtMoTa.Text;
                obj.DVT = txtDVT.Text;
                obj.DonGia = float.Parse(txtDonGia.Text);
                obj.MaLoaiHang = int.Parse(ddlLoaiHang.SelectedValue);
                obj.MaNhaCungCap = int.Parse(ddlNhaCungCap.SelectedValue);
                obj.HinhAnh = imgageFile;

                _DMHangBL.updateDMHang(obj);
            }

            Response.Redirect("~/Admin/DMHangManager.aspx");
        }
    }
}